/**
 * 渠道下拉选择类
 * 
 * 参数 params { 
 * 		checkNode  : 要回显的节点
 * 		onCheck    ：{  },
 *      target     : '',
 *      title      : '',
 *      url        : '',
 *     }
 * 
 * @author fb
 */
window.mofang.core.selectGroup = function(_params) {
	var params = _params;
	var ztree = null;
	var treeCache = null;
	var version = null;
	var target = null;

	var show = function() {
		version = new Date().getTime();
		target = $('#' + params.target);
		treeInit();
		eventInit();
	}; 

	var treeInit = function() {
		treeCache = {};
		target.parent().append(createMenuContent());
		util.mofangAjax({
			url : params.url.query,
			succFun : function(json) {
				// 数据格式转化iconSkin:'icon-process',
				var top = {
					name : params.title + "根结点",
					icon : 'css/zTreeStyle/img/diy/1_open.png',
					open : true,
					nocheck : true,
					children : []
				};
				$.each(json.data, function(i, data) {
					var node = {};
					node['id'] = data.id;
					node['name'] = data.name;
					treeCache[data.id] = data;

					if (data.children && data.children.length > 0) {
						node['open'] = true;
						node['children'] = [];
						dataFormat(data.children, node);
					}

					top.children.push(node);
				});

				// ztree控件
				var setting = {
					check: {enable: true, chkStyle: "radio", radioType: "all" },
					view: { dblClickExpand: false },
					callback: {
						onClick: function(e, treeId, treeNode){
							ztree.checkNode(treeNode, !treeNode.checked, null, true);
							return util.stopDefault(e);
						} ,
						onCheck: function(e, treeId, treeNode) {
									target.val(treeNode.name);
									if (params.onCheck) {
										params.onCheck(treeNode);
									}
									return util.stopDefault(e);
						}
					}

				};

				ztree = $.fn.zTree.init($('#' + getId('groupTree')), setting, [ top ]);
				var checkNode = ztree.getNodeByParam('id',params.checkNode.id, null);
				params.checkNode && ztree.checkNode(checkNode, !checkNode.checked, null, true);
			}
		});
	};

	var dataFormat = function(childrens, parent) {
		$.each(childrens, function(i, children) {
			var node = {};
			node['id'] = children.id;
			node['name'] = children.name;
			treeCache[children.id] = children;

			if (children.children && children.children.length > 0) {
				node['open'] = true;
				node['children'] = [];
				dataFormat(children.children, node);
			}

			parent.children.push(node);
		});
	};
	
	var createMenuContent = function(){
		return 	'<div id="'+ getId('menuContent') +'" class="dropdown-menu" style="display:none; ">' +
					'<ul id="'+getId('groupTree')+'" class="ztree" style="margin-top:0; min-width:180px; height: 300px;"></ul>' +
			    '</div>';
	};

	var getId = function(str) {
		return str += version;
	};
	
	var eventInit = function() {
		var menuContent = $("#" + getId('menuContent'));
		
		target.unbind('click').bind('click',function(e){
			var position = target.position();
			menuContent.css({left: position.left + "px", top: position.top + target.outerHeight() + 'px'}).slideDown("fast");
			return util.stopDefault(e);
		});
		
		/*target.parentsUntil('from').unbind('mousedown').bind('mousedown',function(e){
			menuContent.fadeOut("fast");
		});*/
		
		var hideSetTime = null;
		menuContent.unbind('mouseout').bind('mouseout',function(e){
			hideSetTime = setTimeout(function(){
				menuContent.fadeOut("fast");
			},500);
		}).unbind('mouseover').bind('mouseover',function(e){
			if (hideSetTime) {
				clearTimeout(hideSetTime);
			}
		});
	};

	return {
		show : show
	};
};